package com.task.sql.schedule.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.task.sql.schedule.common.ApiResult;
import com.task.sql.schedule.common.PageResult;
import com.task.sql.schedule.constant.ResultCode;
import com.task.sql.schedule.domain.entity.SqlTaskGroup;
import com.task.sql.schedule.mapper.SqlTaskGroupMapper;
import com.task.sql.schedule.service.SqlTaskGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.task.sql.schedule.domain.qo.SqlTaskGroupQO;
import com.task.sql.schedule.domain.vo.SqlTaskGroupVO;
import com.task.sql.schedule.domain.dto.SqlTaskGroupDTO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;


/**
 1.  服务实现类
 2.  3. @author donghai.liu
 4. @time 2024-09-10
 */
@Service
public class SqlTaskGroupServiceImpl extends ServiceImpl<SqlTaskGroupMapper, SqlTaskGroup> implements SqlTaskGroupService {

@Resource
private SqlTaskGroupMapper mapper;

@Override
public PageResult<SqlTaskGroupVO> page(SqlTaskGroupQO qo) {
    List<SqlTaskGroupVO> data = mapper.listSqlTaskGroup(qo);
    int total = mapper.countSqlTaskGroup(qo);
    PageResult<SqlTaskGroupVO> pageResult = PageResult.success(data, total);
    pageResult.setPageSize(qo.getPageSize());
    pageResult.setPageNum(qo.getPageNum());
    int pages = total % qo.getPageSize() == 0 ? total / qo.getPageSize() : total / qo.getPageSize() + 1;
    pageResult.setPages(pages);
    return pageResult;
}

@Override
@Transactional
public ApiResult<Long> save(SqlTaskGroupDTO dto) {
    SqlTaskGroup entity = new SqlTaskGroup();
    BeanUtil.copyProperties(dto, entity);
    mapper.insert(entity);
    return ApiResult.success(entity.getId());
}

@Override
@Transactional
public ApiResult<Long> update(SqlTaskGroupDTO dto) {
    if(null == dto.getId()) {
        return ApiResult.error(ResultCode.PARAM_ERROR);
    }
    SqlTaskGroup entity = new SqlTaskGroup();
    BeanUtil.copyProperties(dto, entity);
    mapper.updateById(entity);
    return ApiResult.success(entity.getId());
}
}
